<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/8/11
 * Time: 15:21
 */

namespace App\Http\Controllers\Web;


use App\Models\Administrator;
use App\Service\AdministratorService;
use Illuminate\Http\Request;
use Illuminate\Http\Response;

class AdminController extends BaseController
{
    public function index()
    {
        $page = $this->request->post('page',1);
        $limit  = $this->request->post('limit',10);
        $params = $this->request->post();
        return AdministratorService::getList($params,'id','DESC',$page,$limit);
    }

    public function add()
    {
        $params = $this->request->post();
        if(!isset($params['username']) || empty(trim($params['username']))){
            return Response()->json(['code'=>201,'msg'=>'用户名不能为空']);
        }
        if(!isset($params['password']) || empty(trim($params['password']))){
            return Response()->json(['code'=>201,'msg'=>'登录密码不能为空']);
        }
        if(!isset($params['name']) || empty(trim($params['name']))){
            return Response()->json(['code'=>201,'msg'=>'名称不能为空']);
        }
        if(!isset($params['confirm_password']) || empty(trim($params['confirm_password']))){
            return Response()->json(['code'=>201,'msg'=>'确认密码不能为空']);
        }
        if(trim($params['confirm_password']) != trim($params['password'])){
            return Response()->json(['code'=>201,'msg'=>'两次密码不一致']);
        }
        if(!isset($params['status']) || empty(intval($params['status']))){
            return Response()->json(['code'=>201,'msg'=>'请选择用户状态']);
        }
        return AdministratorService::add($params);
    }

    public function getUserById()
    {
        $userId = $this->request->post('id');
        if(!$userId){
            return Response()->json(['code'=>201,'msg'=>'参数缺失']);
        }
        return AdministratorService::getUserById($userId);
    }

    public function edit()
    {
        $params = $this->request->post();
        if(!isset($params['id']) || empty(intval($params['id']))){
            return Response()->json(['code'=>201,'msg'=>'参数缺失']);
        }
        if(!isset($params['username']) || empty(trim($params['username']))){
            return Response()->json(['code'=>201,'msg'=>'用户名不能为空']);
        }
        if(!isset($params['name']) || empty(trim($params['name']))){
            return Response()->json(['code'=>201,'msg'=>'名称不能为空']);
        }
        if(!isset($params['status']) || empty(intval($params['status']))){
            return Response()->json(['code'=>201,'msg'=>'请选择用户状态']);
        }
        if(!empty($params['password'])){
            if(!isset($params['confirm_password']) || empty(trim($params['confirm_password']))){
                return Response()->json(['code'=>201,'msg'=>'确认密码不能为空']);
            }
        }
        return AdministratorService::edit($params);
    }
    
    public function checkStatus()
    {
        $params = $this->request->post();
        if(!isset($params['id']) || empty(intval($params['id']))){
            return Response()->json(['code'=>201,'msg'=>'参数缺失']);
        }
        if(!isset($params['status']) || empty(intval($params['status']))){
            return Response()->json(['code'=>201,'msg'=>'请选择用户状态']);
        }
        return AdministratorService::checkStatus($params);
    }

    public function destroy()
    {
        $ids = $this->request->post('ids');
        $ids = is_array($ids) ? $ids : [$ids];
        if(!$ids){
            return Response()->json(['code'=>201,'msg'=>'请选择要删除的管理员']);
        }
        return AdministratorService::destroy($ids);
    }
}
